<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="60%" append-to-body custom-class="import-dialog">
      <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent>
          <el-form-item label="房间" prop="premisesAddress">
            <el-input v-model="queryParams.premisesAddress" placeholder="请输入房间" clearable style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" ref="table">
          <!-- <el-table-column label="房间编号" align="center" prop="houseId" /> -->
          <el-table-column label="房间" align="center" prop="premisesAddress"/>

          <el-table-column label="租住周期" align="center" prop="checkInTime">
            <template slot-scope="scope">
              <span v-if="scope.row.checkInTime && scope.row.checkOutTime">{{ scope.row.checkInTime|formatDate('YYYY-MM-DD') }} 至 {{ scope.row.checkOutTime|formatDate('YYYY-MM-DD') }}</span>
              <span v-else>暂无数据</span>
            </template>
          </el-table-column>
          <el-table-column label="承租信息" align="center" prop="reserveUser" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button @click="selectHosue(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { billList } from '@/api/bill/houseBill'

export default {
  props: {
    title: {
      default: '选择房间'
    },
    value: {
      default: false
    }
  },
  dicts: [],
  data() {
    return {
      visible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        estateName: undefined,
        buildingName: undefined,
        roomNumber: undefined,
        apartmentType: undefined,
        editStatus: undefined,
        apartmentState: undefined,
        updateBy: undefined
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: []
    }
  },
  watch: {
    value(newVal, oldVal) {
      if (newVal) {
        this.init()
      }
      this.visible = newVal // 监测value的变化，并赋值。
    },
    visible(val) {
      this.$emit('input', val) // 此处监测visible目的为关闭弹窗时，重新更换value值，注意emit的事件一定要为input。
    }
  },
  methods: {
    init() {
      this.reset()
      this.getList()
    },
    handleQuery() {
      this.getList()
    },
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        estateName: undefined,
        buildingName: undefined,
        roomNumber: undefined,
        apartmentType: undefined,
        editStatus: undefined,
        apartmentState: undefined,
        updateBy: undefined
      }
      this.resetForm('queryForm')
    },
    resetQuery() {
      // this.dateRange = [];
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 查询房间列表 */
    getList() {
      this.loading = true
      billList(this.queryParams).then((response) => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleSelectionChange() {
    },
    selectHosue(item) {
      this.$emit('success', item)
      this.close()
    },
    close() {
      this.reset()
      this.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
  max-height: 88vh !important;
  overflow-y: auto;
}
</style>
